package com.example.oauth2.base.repository;

import com.example.oauth2.base.domain.SsoPermission;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SsoPermissionRepository  extends JpaRepository<SsoPermission,Long> {

    @Query(value = "SELECT\n" +
            "  p.*\n" +
            "FROM\n" +
            "  sso_user AS u\n" +
            "  LEFT JOIN sso_user_role AS ur\n" +
            "    ON u.id = ur.user_id\n" +
            "  LEFT JOIN sso_role AS r\n" +
            "    ON r.id = ur.role_id\n" +
            "  LEFT JOIN sso_role_permission AS rp\n" +
            "    ON r.id = rp.role_id\n" +
            "  LEFT JOIN sso_permission AS p\n" +
            "    ON p.id = rp.permission_id\n" +
            "WHERE u.id = ?1",nativeQuery = true)
    public List<SsoPermission> selectByUserId(String userId);
}
